package com.yourwork.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yourwork.service.LoginService;

@Controller
@RequestMapping(value="/login")
//登陆
public class LoginController {
	@Autowired
	@Qualifier(value="loginService")
	private LoginService loginService;


	@RequestMapping(value="/select",method={RequestMethod.GET})
	public String dl(HttpSession session,Model m){
		System.out.println("进入");
		String result = (String) session.getAttribute("result");
		m.addAttribute("result", result);
		session.setAttribute("result", "");
		//return "NewFile";
		System.out.println("运行成功");
		return "/login/login";
	}
	//登陆判断
	@RequestMapping(value="/select",method={RequestMethod.POST})
	public String login(@RequestParam(value="number")String number,
		@RequestParam(value="password") String password,HttpSession session,
		Model m){
	String result;//存储账号密码验证的结果
	String resultJudge;//存储结果判断后返回的地址;
	result = loginService.loginSelect(number, password);//验证
	resultJudge=loginService.loginJudge(result);//返回地址
	session.setAttribute("managerNumber", number);//将员工工号存储到sesson
	session.setAttribute("result", result);
	session.setMaxInactiveInterval(-1);//将sesson时间设置为永久
	m.addAttribute("result", result);
	return resultJudge;
}
/**
 * 修改密码
*/
@RequestMapping(value="/changePassword",method={RequestMethod.POST})
public String doChangePassword(String number,
		@RequestParam("password") String password, 
		@RequestParam("newPassword_1") String newPassword_1,
		@RequestParam("newPassword_2") String newPassword_2,
		HttpSession session,Model m){
	//将用户名储存到session
	number=(String) session.getAttribute("managerNumber");
	//修改密码
	String result = loginService.changePassword(number,password,newPassword_1,newPassword_2);
	m.addAttribute("result", "密码修改成功请重新登录");
	if(result=="密码修改成功"){
	session.invalidate();}
	return loginService.forgotPasswordResult(result);
}

/**
 * 注销用户
 * */
	@RequestMapping(value="/cancellation",method={RequestMethod.GET})
	public String cancellation(HttpSession session,Model m){
		String result = loginService.cancellation(session);
		m.addAttribute("result", result);
		return "/login/login";
	}

}
